<?php
  date_default_timezone_set('Asia/Kolkata');
  session_start();

  if($_SERVER['HTTP_HOST'] == 'localhost') {
    ini_set('display_errors','On');
    error_reporting(E_ALL);
    $host ='localhost';
    $username = 'root';
    $password = '';
    $dbName = 'nb158f';
  } else {
    ini_set('display_errors','Off');
    $host ='localhost';
    $username = 'live_username';
    $password = 'live_password';
    $dbName = 'live_dbname';
  }


  $link = mysql_connect($host, $username, $password) or die(mysql_error());
  mysql_select_db($dbName)or die(mysql_error());


  ///////////////////////////////////////////////////
  /**
   * replacement of print_r()
   **/
  function pr($data){

//return false;

    echo '<pre style="color:#FFF;background-color:#000;font-size:20px;">';
    print_r($data);
    echo '</pre>';

  }
  /**
   * replacement of print_r()
   **/
  function pr1($data){

//return false;

    echo '<pre style="color:red;background-color:#000;font-size:20px;">';
    print_r($data);
    echo '</pre>';

  }
  /////////////////////////////////////////////////////
  /***
   * @param $tableName STRING "Table Name"
   * @param $data ARRAY "Array of Data posted by form"
   * @return INT Last Inserted ID of Table
   * USAGE : $id = insert('users', $_POST);
   * */
  function insert($tableName='', $data=array())
  {
    $insertQuery = "INSERT INTO `$tableName` SET";

    foreach ($data as $colName=>$value)
    {
      $insertQuery .=" `$colName`='$value',";
    }

    $insertQuery = rtrim($insertQuery, ' ,');

    pr($insertQuery);

    mysql_query($insertQuery) or die(mysql_error());
    return mysql_insert_id();
  } // end insert

  /***
   * @param $tableName STRING "Table Name"
   * @param $data ARRAY "Array of Data posted by form"
   * @param $where STRING "WHERE id=5"
   * @return INT Affected Rows
   * USAGE : $affected_rows = update('users', $data, $where);
   * */
  function update($tableName='', $data=array(),$where='')
  {
    if($where == ''){
      return false;
    }

    $updateQuery = "UPDATE `$tableName` SET";

    foreach ($data as $colName=>$value)
    {
      $updateQuery .=" `$colName`='$value',";
    }
    $updateQuery = rtrim($updateQuery, ' ,');
    $updateQuery .= " $where";
    pr($updateQuery);

    mysql_query($updateQuery) or die(mysql_error());
    return mysql_affected_rows();
  } // end insert

  /***
   * @param $tableName STRING "Table Name"
   * @param $where STRING "WHERE id=5"
   * @return MYSQL RESOURCE
   * USAGE : $result = select('users', 'WHERE id=5');
   * */
  function findAll($tableName='', $where='', $limit='',$orderBy='')
  {
    $selectQuery = "SELECT * FROM `$tableName` $where  $orderBy $limit";
    pr($selectQuery);
    $result = mysql_query($selectQuery) or die(mysql_error());
    return $result;
  } // end select



  /***
   * @param $tableName STRING "Table Name"
   * @param $id INT
   * @return MYSQL RESOURCE
   * USAGE : $result = findById('users', 5);
   * */
  function findById($tableName='', $id=0)
  {
    $selectQuery = "SELECT * FROM `$tableName` WHERE `id`='$id'";
    pr($selectQuery);
    $result = mysql_query($selectQuery) or die(mysql_error());
    return mysql_fetch_assoc($result);
  } // end select



  /***
   * @param $tableName STRING "Table Name"
   * @param $where STRING "WHERE id=5"
   * @return INT Number Of records
   * USAGE : $count = countAll('users', "WHERE name='ravi'");
   * */
  function countAll($tableName='', $where='')
  {
    $selectQuery = "SELECT COUNT(*) as `total` FROM `$tableName` $where";
    //pr($selectQuery);
    $result = mysql_query($selectQuery) or die(mysql_error());
    $data = mysql_fetch_assoc($result);
    //pr($data);
    return $data['total'];
  } // end select


  function getFileName($path='')
  {
    $arr = explode('/',$path);
    $fileName = array_pop($arr);
    return $fileName;
  }

  /////////////////////////////////////////////////////
